package com.cool.controller.sysmanagecontroller;

import com.cool.bo.sysmanage.common.PageQueryBo;
import com.cool.bo.sysmanage.role.AllocResource;
import com.cool.bo.sysmanage.stuff.UserWithRoleBo;
import com.cool.common.CommonResult;
import com.cool.common.ResponseCode;
import com.cool.po.sysmanagepo.MenuPo;
import com.cool.po.sysmanagepo.PermissionPo;
import com.cool.po.sysmanagepo.RolePo;
import com.cool.service.sysmanage.DdRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 角色表(DdRole)表控制层
 *
 * @author Jungle
 * @since 2020-07-27 17:30:55
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class DdRoleController {
    /**
     * 服务对象
     */
    @Resource
    private DdRoleService ddRoleService;

    /**
     * 更新用户角色
     *
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/27
     */
    @PostMapping("/update")
    public CommonResult updateStuff(UserWithRoleBo userWithRoleBo){
        String adminId = userWithRoleBo.getAdminId();
        String roleIds = userWithRoleBo.getRoleIds();
        Integer len = ddRoleService.updateUserRole(adminId,roleIds);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.ROLE_QUERY_FAILURE).setMsg("查询失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("成功");
        }
    }

    /**
     * 得到用户角色
     *
     * @param id id
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/28
     */
    @GetMapping("/getRole/{id}")
    public CommonResult getUserRole(@PathVariable Integer id){
        List<RolePo> list = ddRoleService.selectListById(id);
        if(Objects.isNull(list)){
            return CommonResult.newCommonResult().setCode(ResponseCode.ROLE_QUERY_FAILURE).setMsg("查询失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("查询成功").setData(list);
        }
    }

    /**
     * 得到所有用户角色
     *
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/28
     */
    @GetMapping("/listAll")
    public CommonResult getAllUserRole(){
        List<RolePo> list = ddRoleService.getAllUserRole();
        if(Objects.isNull(list)){
            return CommonResult.newCommonResult().setCode(ResponseCode.ROLE_QUERY_FAILURE).setMsg("查询失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("查询成功").setData(list);
        }
    }

    /**
     * 分页查询
     *
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/28
     */
    @GetMapping("/list")
    public CommonResult pagingQuery(RolePo rolePo){

        PageQueryBo pageQueryBo = ddRoleService.pagingQuery(rolePo);

        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("分页查询成功").setData(pageQueryBo);
    }

    /**
     * 更新状态
     *
     * @param id    id
     * @param state 状态
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/28
     */
    @PostMapping("/updateStatus/{id}")
    public CommonResult updateStatus(@PathVariable Integer id,@RequestParam("status")Integer state){
        Integer len = ddRoleService.updateStatus(id,state);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.ROLE_INSERT_FAILURE).setMsg("更新失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("更新成功");
        }
    }

    /**
     * 更新状态
     *
     * @param id     id
     * @param rolePo 角色
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/28
     */
    @PostMapping("/updateRole/{id}")
    public CommonResult updateStatus(@PathVariable Integer id,@RequestBody RolePo rolePo){
        Integer len = ddRoleService.updateRole(id,rolePo);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.ROLE_INSERT_FAILURE).setMsg("更新失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("更新成功");
        }
    }

    /**
     * 创建
     *
     * @param rolePo 角色
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody RolePo rolePo){

        if(StringUtils.isEmpty(rolePo.getRoleName())){
            return CommonResult.newCommonResult().setCode(ResponseCode.ROLE_INSERT_FAILURE).setMsg("无角色名");
        }

        Integer len = ddRoleService.insert(rolePo);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.ROLE_INSERT_FAILURE).setMsg("创建失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("创建成功");
        }
    }

    /**
     * 删除
     *
     * @param ids id
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @PostMapping("/delete")
    public CommonResult delete(String ids){

        Integer len = ddRoleService.deleteById(ids);

        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.ROLE_INSERT_FAILURE).setMsg("删除失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("删除成功");
        }

    }

    /**
     * 把列表菜单
     *
     * @param id id
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @GetMapping("/listMenu/{id}")
    public CommonResult getStuffListMenu(@PathVariable("id")Integer id){

        List<MenuPo> list = ddRoleService.selectMenuList(id);

        if(Objects.isNull(list) || list.size()< 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("当前用户拥有菜单暂无");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("查询成功").setData(list);

        }

    }

    /**
     * 更新alloc菜单
     *
     * @param roleId  角色id
     * @param menuIds 菜单id
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @PostMapping("/allocMenu")
    public CommonResult updateAllocMenu(String roleId,String menuIds){
        try{
            Integer let = ddRoleService.updateAllocMenu(roleId,menuIds);
            if(let < 1){
                return CommonResult.newCommonResult().setCode(ResponseCode.MENU_UPDATE_FAILURE).setMsg("更新菜单失败");
            }else {
                return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("更新菜单成功");

            }
        }catch (RuntimeException e){
            return CommonResult.newCommonResult().setCode(ResponseCode.MENU_UPDATE_FAILURE).setMsg(e.getMessage());

        }



    }

    /**
     * 用户权限
     *
     * @param id id
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @GetMapping("/listResource/{id}")
    public CommonResult getUserPermissionByStuffId(@PathVariable("id")Integer id){
        List<PermissionPo> list =  ddRoleService.getUserPermissionByRoleId(id);
        if(Objects.isNull(list)){
            return CommonResult.newCommonResult().setCode(ResponseCode.ROLE_PERMISSION_QUERY_FAILURE).setMsg("用户拥有角色查询失败");

        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("查询成功").setData(list);

        }
    }

    /**
     * 分配资源
     *
     * @param allocResource 分配资源
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @PostMapping("/allocResource")
    public CommonResult allocResource(AllocResource allocResource){

        Integer len = ddRoleService.allocResource(allocResource);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.MENU_ALLOC_FAILURE).setMsg("分配资源失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("分配资源成功");

        }
    }

}